/*
 * Nirvana Room Inherit
 * Authored by Vertebraker@Nirvana
 * <tmieczkowski@hotmail.com>
 * Started: 8-27-03
 *
 */ 

#include "/room/clean.c"

/* An array with destinations and directions: "room/church", "north" ... */
string *dest_dir;

/* Short description of the room */
string short_desc;

/* Long description of the room */
string long_desc;

/* Special items in the room. "table", "A nice table", "window", "A window" */
string *items;

/* Fact about this room. ex: "no_fight", "no_steal" */
mixed property;

/* No castles are allowed to be dropped here */
int no_castle_flag;

/* No exits shown flag */
int no_exits;

/* Custom exits flag */
int custom_exits;

/* Handling number of exits */
string *numbers;

#include "/room/lib/func.h"
#include "/room/lib/sensory.h"

void init()
{
 int i;
 object enteringPlayer;

 if (!dest_dir)
  return;

 if(this_object()->query_property("fight_area") &&
  (enteringPlayer=this_player()) && enteringPlayer->is_player())
  enteringPlayer->set_fight_area();

 if(this_object()->query_property("spar_area") &&
  (enteringPlayer=this_player()) && enteringPlayer->is_player())
  enteringPlayer->set_spar();

 i = 1;
 while(i < sizeof(dest_dir))
 {
  add_action("move", dest_dir[i]);
  i += 2;
 }
}

status id(string str)
{
 if (!items) return 0;
 return (member_array(str, items) > -1);
}

varargs void long(string str)
{
 int i;
 if ((set_light(0) <= 0) && !present("dark_sight_object", this_player()))
 {
  write("It is dark.\n");
  return;
 }
 if (!str)
 {
  write(long_desc);
  if(custom_exits)
   return;
  if(!dest_dir || no_exits)
   write("    No obvious exits.\n");
  else
  {
   int s;
   i = 1;
   if ((s=sizeof(dest_dir))==2)
    write("\
    There is one obvious exit:");
   else
    write("\
    There are "+(string)this_object()->convert_number(s/2)+" obvious exits:");
   while(i < s)
   {
    write(" " + dest_dir[i]);
    i += 2;
    if (i == s-1)
     write(" and");
    else if (i < s)
     write(",");
   }
   write("\n");
  }
  return;
 }
 if (!items)
  return;
 if((i = member_array(str, items)) > -1)
  write(items[i+1]+".\n");
}

/*
 * Does this room has a special property ?
 * The 'property' variable can be both a string and array of strings.
 * If no argument is given, return the 'property' variable.
 */

mixed query_property(string str)
{
 int i;

 if (!str)
  return property;
 if (!property)
  return 0;
 if (stringp(property))
  return str == property;

 if(pointerp(property))
 return (member_array(str, property) > -1);
}

void set_property(mixed str)
{
 property = str;
}

void add_property(mixed str)
{
 if(!str)
  return;
 if(!property)
  property=str;
 if(stringp(property))
  property=({ property, str });
 if(pointerp(property))
  property += ({ str });
}

void remove_property(mixed str)
{
 if(!str)
  return;
 if(stringp(property) && property==str)
  property=0;
 if(pointerp(property))
 {
  if(member_array(str, property) > -1)
   property -= ({ str });
  if(!sizeof(property))
   property = 0;
 }
}

status move(string str)
{
 int i;
 if((i = member_array(query_verb(), dest_dir)) > -1)
 {
  this_player()->move_player(dest_dir[i] + "#" + dest_dir[i-1]);
  return 1;
 }
}

string short()
{
 int i, s;
 string temp;
 /* <= conversion by verte 9.14.01 */
 if ((set_light(0) > 0) || (this_player() && environment(this_player()) == this_object() && present("dark_sight_object", this_player())))
 {
  temp = short_desc + " [";
  if (!dest_dir || no_exits) temp += "no exits";
  else if ((s=sizeof(dest_dir)) >= 12) temp += "many exits";
  else
   for (i=1;i<s;i+=2)
   {
    temp += (string)this_object()->translate_exit(dest_dir[i]);
    if (i < s-2)
     temp += ",";
   }
  temp += "]";
  return temp;
 }
 return "Dark room";
}

string translate_exit(string fullDirection)
{
 switch(fullDirection)
 {
  case "north":     return "n";
  case "south":     return "s";
  case "east":      return "e";
  case "west":      return "w";
  case "northeast": return "ne";
  case "northwest": return "nw";
  case "southeast": return "se";
  case "southwest": return "sw";
  case "up":        return "u";
  case "down":      return "d";
  default:          return fullDirection;
 }
}

status query_drop_castle() { return no_castle_flag; }
string query_short() { return short_desc; }
string * query_dest_dir() { return dest_dir; }
string query_long() { return long_desc; }

string convert_number(int inputNum)
{
 if (!pointerp(numbers))
  numbers = (string*)this_object()->query_numbers();
 if (inputNum > 9)
  return "lot of";
 return numbers[inputNum];
}

string * query_numbers()
{
 if (!numbers)
 {
  if (file_name(this_object()) == "room/room")
   numbers = ({"no", "one", "two", "three", "four", "five",
	         "six", "seven", "eight", "nine" });
  else
   numbers = (string*)"/room/room"->query_numbers();
 }
 return numbers;
}

string realm()
{
 if(query_property("no_teleport")) return "NT";
 if(query_property("no_follow")) return "LA";
}

status okay_follow()
{
 if(realm() == "NT" || query_property("no_follow")) return 0;
 return 1;
}

varargs status exit(object exitingPlayer)
{
 if(query_property("fight_area") && exitingPlayer)
  exitingPlayer->clear_fight_area();
 if(query_property("spar_area") && exitingPlayer)
  exitingPlayer->rm_spar();
}

status query_no_fight() { return query_property("no_fight"); }
status query_spar_area() { return query_property("spar_area"); }
status query_NM() { return query_property("no_magic"); }

reset() { }  /* left in for solidarity */

